package com.wenwu.controller.unmovable;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwu.common.bean.RelicsPreserve;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.service.BasicService;
import com.wenwu.service.ProtectSatuationService;
/**
 * 保存现状
 * 
 * @author LINHAI
 *
 */
// Json数据传输
// 日志打印
/**
	 * 保存现状查询
	 * 
	 * @param basicId
	 * @return
	 */
/**
	 * 添加保存现状
	 */
// 异常处理
/**
	 * 分页查询RelicsPreserve
	 * 
	 * @param relicsPreserve
	 * @param request
	 * @return
	 * @throws Exception
	 */
// 查询符合条件的个数
// 查询符合条件的数据
/**
	 * 删除保存现状
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
/**
 * 保存现状
 * 
 * @author LINHAI
 *
 */
// Json数据传输
// 日志打印
/**
	 * 保存现状查询
	 * 
	 * @param basicId
	 * @return
	 */
/**
	 * 添加保存现状
	 */
// 异常处理
/**
	 * 分页查询RelicsPreserve
	 * 
	 * @param relicsPreserve
	 * @param request
	 * @return
	 * @throws Exception
	 */
// 查询符合条件的个数
// 查询符合条件的数据
/**
	 * 删除保存现状
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
/**
 * 保存现状
 * 
 * @author LINHAI
 *
 */
@Controller(value = "ProtectSatuationController")
@RequestMapping(value = "/mgr/umprotectsatu")
public class ProtectSatuationController {

	@Autowired
	private ProtectSatuationService protectSatuationService;
	@Autowired
	private BasicService basicService;

	// Json数据传输
	private JsonGenerator jsonGenerator = null;
	// 日志打印
	Logger log = Logger.getLogger(this.getClass());

	@RequestMapping(value = "/init")
	public String initProtectSatuPage() {
		log.info("进入保存现状的页面");
		return "umprotectsatu/umprotectsatulist";
	}

	@RequestMapping(value = "/umprotectsatuform")
	public String umprotectsatuform(Model model,
			@RequestParam(required = false, value = "basicId") Integer basicId,
			@RequestParam(required = false, value = "name") String name) {
		log.info("进入保存现状的页面");
		log.info("进入表单页面...");
		try {
			if (StringUtils.isNotBlank(name)) {
				model.addAttribute("name", URLDecoder.decode(name, "UTF-8"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "umprotectsatu/umprotectsatuform";
	}

	/**
	 * 保存现状查询
	 * 
	 * @param basicId
	 * @return
	 */
	@RequestMapping(value = "/showUpdate")
	public @ResponseBody RelicsPreserve selectProtectSatuationByBasicId(
			@RequestParam(required = false, value = "basicId") Integer basicId) {

		RelicsPreserve relicsPreserve = null;
		log.debug("获得保存现状查询主键basicId===" + basicId);
		if (basicId != null) {
			try {
				relicsPreserve = protectSatuationService
						.selectRelicsPreserveByBasicId(basicId);
			} catch (RelicsException e) {
				e.printStackTrace();
			}
		}
		return relicsPreserve;
	}

	/**
	 * 添加保存现状
	 */
	@RequestMapping(value = "/addProtectSatu")
	public void addProtectSatu(RelicsPreserve relicsPreserve,
			HttpServletResponse response) throws Exception {

		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加文物现状");
		try {
			jsonGenerator.writeObject(protectSatuationService
					.addObj(relicsPreserve));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
			jsonGenerator.writeObject(e.getMessage());
		}
	}
	
	@RequestMapping(value = "/showThisUpdate")
	public @ResponseBody RelicsPreserve showThisUpdate(
			@RequestParam(required = false, value = "preserveId") Integer preserveId)
					throws Exception {
		RelicsPreserve relicsPreserve = null;
		try {
			if (preserveId != null) {
				relicsPreserve = protectSatuationService.selectRelicsPreserveByPreserveId(preserveId);
			}
		} catch (Exception e) {
		}
		return relicsPreserve;
	}
	/**
	 * 分页查询RelicsPreserve
	 * 
	 * @param relicsPreserve
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/protectsatuList")
	public @ResponseBody PaginationVO showList(RelicsPreserve relicsPreserve,
			HttpServletRequest request) throws Exception {
		log.debug("查询RelicsPreserve集合");
		PaginationVO pageVo = null;
		try {
			pageVo = PageUtils.getPaginationInfo(request);
			// 查询符合条件的个数
			pageVo= protectSatuationService.selectList(relicsPreserve, pageVo);// 查询符合条件的数据
			System.out.println("查询RelicsPreserve集合pageVo="+pageVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageVo;
	}

	/**
	 * 删除保存现状
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteProtectSatuInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "key") String key)
			throws Exception {
		log.info("保存现状主键：" + key);
		if (key != null) {
			List<String> keys = new ArrayList<String>();
			String[] objs = key.split(",");
			for (String k : objs) {
				keys.add(k);
			}
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			try {
				int result = protectSatuationService.deleteByKey(keys);
				jsonGenerator.writeObject("成功删除" + result + "条数据");
			} catch (Exception e) {
				jsonGenerator.writeObject("删除数据失败");
			}
		}
	}
}
